package dsk.demo.miaosha.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dsk.demo.miaosha.demo.pojo.Order;
import dsk.demo.miaosha.demo.pojo.SeckillOrder;
import dsk.demo.miaosha.demo.pojo.User;
import dsk.demo.miaosha.demo.rabbitmq.MQSender;
import dsk.demo.miaosha.demo.rabbitmq.SeckillMessage;
import dsk.demo.miaosha.demo.service.IGoodsService;
import dsk.demo.miaosha.demo.service.IOrderService;
import dsk.demo.miaosha.demo.service.ISeckillOrderService;
import dsk.demo.miaosha.demo.vo.GoodsVo;
import dsk.demo.miaosha.demo.vo.RespBean;
import dsk.demo.miaosha.demo.vo.RespBeanEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/seckill")
public class SecKillController implements InitializingBean {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    MQSender mqSender;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping(value = "/doSeckill",method = RequestMethod.POST)
    @ResponseBody
    public RespBean doSecKill(User user, Long goodsId) {
        if(user == null) {
            return RespBean.error(RespBeanEnum.USER_EMPTY);
        }
//        GoodsVo goods = goodsService.findGoodsVoById(goodsId);
        //
        //判断是否重复抢购
        String seckillOrder =(String) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
        if(!StringUtils.isEmpty(seckillOrder)) {
            return RespBean.error(RespBeanEnum.REPEATE_ERROR);
        }
//        //预存订单是否存在
//        String preOrder =(String) redisTemplate.opsForValue().get("preOrder:" + user.getId() + ":" + goodsId);
//        if(!StringUtils.isEmpty(preOrder)) {
//            return RespBean.error(RespBeanEnum.REPEATE_ERROR);
//        }
        Long decrement = redisTemplate.opsForValue().decrement("seckillGoods:" + goodsId);
        if(decrement<0){
            redisTemplate.opsForValue().increment("seckillGoods:" + goodsId);
            return RespBean.error(RespBeanEnum.EMPTY_STOCK);
        }
        //假如用户抢到了
        //redis设置预存订单
//        redisTemplate.opsForValue().set("preOrder:" + user.getId() + ":" + goodsId,"1",600, TimeUnit.SECONDS);

        //发送消息给消息队列
//        Order order = orderService.seckill(user,goodsId);
        mqSender.sendSeckillMessage(new SeckillMessage(user,goodsId));
        return RespBean.success(0);
    }

    /**
     * 初始化商品库存
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        List<GoodsVo> list = goodsService.findGoodsVo();
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        list.forEach(goodsVo -> {
            redisTemplate.opsForValue().set("seckillGoods:"+goodsVo.getId(),goodsVo.getStockCount());
        });
    }
}
